package com.applet.code.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * 时间工具类
 * 
 * @author 80002202
 *
 */
public class DateUtils {

	private DateUtils() {}


	public final static SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
	public final static SimpleDateFormat yyyyMMddHHmmssFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	public final static SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 例：2018-01-01
	 */
	public final static String tF = "%tF";
	
	/**
	 * 例：2018-01-01 11:11:22
	 */
	public final static String tFT = "%tT";

	/**
	 * 将java.util.Date转换为相关格式字符串
	 * 
	 * @param _date
	 * @return
	 */
	public static String format(Date date, SimpleDateFormat dateFormat) {
		synchronized(dateFormat){
			return dateFormat.format(date);
		}
	}
	
	/**
	 * 将java.util.Date转换为相关格式字符串
	 * 
	 * @param _date
	 * @return
	 */
	public static String format(Date date, String format) {
		return String.format(format, date);
	}

	/**
	 * 将字符串转为 java.util.Date 类型
	 * 
	 * @param _source
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String source, SimpleDateFormat dateFormat) {
		synchronized(dateFormat){
			try {
				return dateFormat.parse(source);
			} catch (ParseException e) {
				return null;
			}
		}
	}

	/**
	 * 获取系统当前日期
	 * 
	 * @return
	 */
	public static Date getNowDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取系统当前时间
	 * 
	 * @return
	 */
	public static Date getNowDateTime() {
		Calendar calendar = Calendar.getInstance();
		return calendar.getTime();
	}

	public static Date add(Date time, int field, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		calendar.add(field, amount);
		return calendar.getTime();
	}

	public static Calendar getCalendar(Date date){
	    Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    return calendar;
	}
	
	public static Date secondToDate(Long millis){
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millis*1000);
		return calendar.getTime();
	}

}
